import Vue from 'vue'
import Router from 'vue-router'
import login from '@/components/login'
import index from '@/components/index'
import set from '@/components/set'
import boxIndex from '@/components/boxIndex'
import delIndex from '@/components/delIndex'
import beliverymanIndex from '@/components/beliverymanIndex'
import towerIndex from '@/components/towerIndex'
import cookie from '../cookie'
import historyOrder from '../components/historyOrder.vue'

Vue.use(Router)
const router =  new Router({
  routes: [
    {
      path:'/',
      redirect:'/set',
    },
    {
      path:'/set',
      name:'set',
      component:set
    },
    {
      path:'/login',
      name:'login',
      component:login
    },
    {
      path:'/index',
      name:'index',
      component:index,
      meta:{
        isLogin:true
      }
    },
    {
      path:'/boxIndex',
      name:'boxIndex',
      component:boxIndex,
      meta:{
        isLogin:true
      }
    },
    {
      path:'/delIndex',
      name:'delIndex',
      component:delIndex,
      meta:{
        isLogin:true
      }
    },
    {
      path:'/beliverymanIndex',
      name:'beliverymanIndex',
      component:beliverymanIndex,
      meta:{
        isLogin:true
      }
    },
    {
      path:'/towerIndex',
      name:'towerIndex',
      component:towerIndex,
      meta:{
        isLogin:true
      }
    },
    {
      path:'/historyOrder',
      name:'historyOrder',
      component:historyOrder,
      meta:{
        isLogin:true
      }
    }
  ]
})
router.beforeEach((to,from,next)=>{
  let roleinfo = JSON.parse(cookie.get('data','roleinfo'));
  if(to.meta.isLogin){
    if(!cookie.get('data','token')){
      next('/set');
      return;
    }
  }
    if(cookie.get('data','token')){
      switch(roleinfo.type){
        case 1:
          if(to.name == "index"){
            next();
          }else{
            next('/index');
          }
          break;
        case 2:
          if(to.name == 'delIndex' || to.name == 'boxIndex' || to.name == 'historyOrder'){
            next();
          }else{
            next('/boxIndex');
          }
          break;
        case 3:
          if(to.name == 'towerIndex' || to.name == 'historyOrder'){
            next();
          }else{
            next('/towerIndex')
          }
          break;
        case 4:
          if(to.name == 'beliverymanIndex'){
            next();
          }else{
            next('/beliverymanIndex');
          }
          break;
        default:
          next('/set');
      }
      return;
    }
  next();
})
export default router;
